/*
 * Copyright (c) 2014 pkzd/1.0, 颜文泽(robin)
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     1 Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     2 Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include	<asm.h>
#include	<term.h>
#include	<param.h>
#include	<string.h>

void	readkern(void);
static void	get_mem_siz(void);
static void	get_disk_param(void);

static void
get_mem_siz(void)
{
	uint i, cnt, *addr;

	if(cnt = *(uint *)MEM_CNT){
		addr = (uint *)MEM_ADDR;
		for(i = 0; i < cnt; i++, addr += (MEM_MAP / sizeof(uint))){
			if(addr[4] == 0x1 && addr[0] == KERN_ADDR){ 
				cnt = addr[0] + addr[2];
				*(uint *)MEM_CNT = RND_PAGE_SIZ(cnt);
				return;
			}
		}	
	}
	mc_spin("Error: can't access memory size.\n");
}

static void
get_disk_param(void)
{
        uchar   n, s;
	ushort	*dp, *ep, port0;

	dp = (ushort *)DRV_PARAM;
	ep = (ushort *)(dp[13] + (dp[14] << 4));
        if(!(port0 = *(ushort *)IDE_CMD = *ep) || !(*(ushort *)IDE_CTRL = *(ep + 1)))
                mc_spin("Only support IDE mode.\n");
	*(uint *)IDE_TWO = 0;
        *(uint *)IDE_SLV = s =  *((uchar *)ep + 4) & 0x10;
	outb(0xe0, port0 + 6);
	n = inb(port0 + 7) & IDE_DSC ? 1 : 0;
	if(!(s && !n)){
		while((inb(port0 + 7) & IDE_MASK) != IDE_DRDY)
			;
                outb(IDE_IDENTIFY, port0 + 7);
		while(inb(port0 + 7) & IDE_BUSY)
			;
                if(inb(port0 + 7) & IDE_ERR)
                        mc_spin("Error: can't access master disk parameters.\n");
                insl((void *)IDE0_PARAM, port0, SECTORSIZ / 4);
	}
	outb(0xf0, port0 + 6);
	n = inb(port0 + 7) & IDE_DSC ? 1 : 0;
	if(!(!s && !n)){
		while((inb(port0 + 7) & IDE_MASK) != IDE_DRDY)
			;
                outb(IDE_IDENTIFY, port0 + 7);
		while(inb(port0 + 7) & IDE_BUSY)
			;	
                if(inb(port0 + 7) & IDE_ERR)
                        mc_spin("Error: can't access master disk parameters.\n");
                insl((void *)IDE1_PARAM, port0, SECTORSIZ / 4);
                *(uint *)IDE_TWO = 1;
        }
        outb(0xe0 | s, port0 + 6);
}

void
setparam(void)
{
	mc_terminit();
	get_mem_siz();
	if(*((uint *)MEM_CNT) < USER_LMT)
		mc_spin("Error: At least 32MB memory size.\n");
	get_disk_param();
	readkern();
	mc_spin("Error: unknow error.\n");
}
